package org.longxin.domains;

// Generated 2015-1-17 22:30:04 by Hibernate Tools 3.4.0.CR1

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.CascadeType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * L1Component generated by hbm2java
 */
@Entity
@Table(name = "l1_component", catalog = "longxin")
public class L1Component extends Component implements java.io.Serializable, Cloneable
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2912980312734388140L;
	private Integer id;
	private Module module;
	private FunctionModule functionModule;
	private String name;
	private String description;
	private String functionName;
	private Boolean template;
	private Set<L1ComponentParameter> l1ComponentParameters = new HashSet<L1ComponentParameter>(
			0);
	private Set<L2Component> l2Components = new HashSet<L2Component>(0);

	private List<L2Component> tempL2Components = new ArrayList<L2Component>();
    
    private List<L1ComponentParameter> tempL1Parameters = new ArrayList<L1ComponentParameter>();
	
	public L1Component()
	{
	}

	public L1Component(String name)
	{
		this.name = name;
	}

	public L1Component(Module module, String name, String description,
			Set<L1ComponentParameter> l1ComponentParameters,
			Set<L2Component> l2Components)
	{
		this.module = module;
		this.name = name;
		this.description = description;
		this.l1ComponentParameters = l1ComponentParameters;
		this.l2Components = l2Components;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "ID", unique = true, nullable = false)
	public Integer getId()
	{
		return this.id;
	}

	public void setId(Integer id)
	{
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "MODULE")
	public Module getModule()
	{
		return this.module;
	}

	public void setModule(Module module)
	{
		this.module = module;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "FUNCTION_MODULE")
    public FunctionModule getFunctionModule()
    {
        return this.functionModule;
    }

    public void setFunctionModule(FunctionModule functionModule)
    {
        this.functionModule = functionModule;
    }

	@Column(name = "NAME", nullable = false, length = 45)
	public String getName()
	{
		return this.name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	@Column(name = "DESCRIPTION", length = 512)
	public String getDescription()
	{
		return this.description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "l1Component", cascade= CascadeType.ALL)
	@OrderBy("id ASC")
	public Set<L1ComponentParameter> getL1ComponentParameters()
	{
		return this.l1ComponentParameters;
	}

	public void setL1ComponentParameters(
			Set<L1ComponentParameter> l1ComponentParameters)
	{
		this.l1ComponentParameters = l1ComponentParameters;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "l1Component", cascade= CascadeType.ALL)
	@OrderBy("id ASC")
	public Set<L2Component> getL2Components()
	{
		return this.l2Components;
	}

	public void setL2Components(Set<L2Component> l2Components)
	{
		this.l2Components = l2Components;
	}
	
	@Column(name = "FUNCTION_NAME")
	public String getFunctionName()
	{
		return functionName;
	}

	public void setFunctionName(String functionName)
	{
		this.functionName = functionName;
	}
	
	@Column(name = "TEMPLATE")
    public Boolean getTemplate()
    {
        return this.template;
    }

    public void setTemplate(Boolean template)
    {
        this.template = template;
    }
    
    @Transient
	public List<L2Component> getTempL2Components()
    {
        return tempL2Components;
    }

    public void setTempL2Components(List<L2Component> tempL2Components)
    {
        this.tempL2Components = tempL2Components;
    }

    @Transient
    public List<L1ComponentParameter> getTempL1Parameters()
    {
        return tempL1Parameters;
    }

    public void setTempL1Parameters(List<L1ComponentParameter> tempL1Parameters)
    {
        this.tempL1Parameters = tempL1Parameters;
    }

    @Override
	protected Object clone() throws CloneNotSupportedException
	{
		L1Component l1Component = (L1Component)super.clone();
		l1Component.setId(null);
		l1Component.setFunctionModule(null);
		l1Component.setTemplate(Boolean.FALSE);
		if (this.l1ComponentParameters != null)
		{
			Set<L1ComponentParameter> clonedL1ComponentsParameters = new HashSet<L1ComponentParameter>();

			for (L1ComponentParameter l1ComponentParameter : l1ComponentParameters)
			{
				L1ComponentParameter clonedL1ComponentsParameter = (L1ComponentParameter) l1ComponentParameter.clone();
				clonedL1ComponentsParameter.setL1Component(l1Component);
				clonedL1ComponentsParameters.add(clonedL1ComponentsParameter);
			}

			l1Component.setL1ComponentParameters(clonedL1ComponentsParameters);
		}

		if (this.l2Components != null)
		{
			Set<L2Component> clonedl2Components = new HashSet<L2Component>();
			
			for(L2Component l2Component : l2Components){
				L2Component clonedl2Component = (L2Component) l2Component.clone();
				clonedl2Component.setL1Component(l1Component);
				clonedl2Components.add(clonedl2Component);
			}
			
			l1Component.setL2Components(clonedl2Components);
		}
		return l1Component;
	}
}
